package main

import "fmt"

type number int

func (n number) print(index int, msg string) { fmt.Println("index(值)", index, n, &n, msg) }

func (n *number) pprint(index int, msg string) { fmt.Println("index(引)", index, *n, n, msg) }

func main() {
	var n number
	fmt.Println("index", 0, n, &n)
	defer n.print(1, "值")                  //打印 n 的值,打印顺序 4
	defer n.pprint(2, "引")                 //打印 n 的指,打印顺序 3
	defer func() { n.print(3, "闭包 1") }()  //打印 n 的值，打印顺序 2
	defer func() { n.pprint(4, "闭包 2") }() //打印 n 的指,打印顺序 1
	defer n.print(5, "值")                  //打印 n 的值,打印顺序 4
	defer n.pprint(6, "引")
	n = 3
	// 准备一个字符串
	str := "hello world"
	// 创建一个匿名函数
	foo := func() {
		// 匿名函数中访问str
		fmt.Println("闭包中打印（修改前）", str)
		str = "hello dude"
	}
	fmt.Println("闭包函数调用前", str)
	str = "hello ionc"
	// 调用匿名函数
	foo()
	fmt.Println("修改后", str)
}
